#pragma once

#include "tcp_server.hpp"

namespace yx
{
    class HttpServer
    {
    private:
        int16_t _port;
        yx::TcpServer *_tcp_server;

    public:
        HttpServer(int port)
            : _port(port), _tcp_server(nullptr)
        {
        }

        ~HttpServer()
        {
        }

        void loop()
        {
            info("loop start!!!\n");

            _tcp_server = yx::pattern::Singleton<yx::TcpServer>::instance(9090);
            int listen_sockfd = _tcp_server->getListenSockfd();

            while (true)
            {
                struct sockaddr_in peer;
                socklen_t socklen = sizeof(peer);
                memset(&peer, 0, socklen);

                int sockfd = accept(listen_sockfd, (struct sockaddr *)&peer, &socklen);
                if (sockfd < 0)
                {
                    continue;
                }
                // 打印客户端相关信息
                std::string client_ip = inet_ntoa(peer.sin_addr);
                int client_port = ntohs(peer.sin_port);
                info("get a new link: [%s : %d]\n", client_ip.c_str(), client_port);

                // ...
            }
        }
    };
}